package sk.isomi.minihr.mngts;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;

import sk.isomi.minihr.entities.Employee;
import sk.isomi.minihr.entities.Manager;

public class EmployeeMngt {
	private ArrayList<Employee> employees;
	
	public EmployeeMngt() {
		employees = new ArrayList<Employee>();
	}
	
	public ArrayList<Employee> getEmployees() {
		return employees;
	}
	
	public boolean addEmployee(Employee employee) {
		return employees.add(employee);
	}
	
	public boolean removeEmployee(Employee employee) {
		return employees.remove(employee);
	}
	
	public boolean removeEmployee(String empId) {
		Employee employee = findById(empId);
		if (employee != null) {
			return removeEmployee(employee);
		}
		return false;
	}
	
	public boolean addManager(Manager manager, Employee employee) {
		if (employee != null) {
			employee.setManager(manager);
			return true;
		}
		return false;	
	}
	
	public double getAverageSalary() {
		double averageSalary = 0.0;
		
		for (Employee employee : employees) {
			averageSalary += employee.getSalary();
		}
		averageSalary /= employees.size();
		BigDecimal decimal = new BigDecimal(averageSalary);
		decimal.setScale(2, RoundingMode.HALF_EVEN);
		
		return decimal.doubleValue();		
	}
	
	public Employee findById(String id) {
		for (Employee employee : employees) {
			if (employee.getId().equals(id)) {
				return employee;
			}
		}
		return null;
	}
	
	public ArrayList<Employee> findByManager(Manager manager) {
		ArrayList<Employee> found = new ArrayList<Employee>();
		
		for (Employee employee : employees) {
			if (manager.getId().equals(employee.getManager().getId())) {
				found.add(employee);
			}
		}
		return found;
	}

	@Override
	public String toString() {
		return String.format("EmployeeMngt [employees count=%d]", employees.size());
	}
	
	
}
